﻿using System;
using System.Collections.Generic;

using System.Web;
using System.IO;
using System.Configuration;
using PIM.Utilities.DataAccess;

using System.Collections;
using System.Data;
using PIM.Utilities.Cryptography;



/// <summary>
/// Summary description for Common
/// </summary>
namespace PIM.API
{
    public class Common
    {
        public Common()
        {
        }
        //public static void Show(string message)
        //{
        //    string cleanMessage = message.Replace("'", "\'");
        //    Page page = HttpContext.Current.CurrentHandler as Page;
        //    string script = string.Format("alert('{0}');", cleanMessage);
        //    if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
        //    {
        //        page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", script, true /* addScriptTags */);
        //    }
        //}

        public class Logger
        {
            static System.Threading.Semaphore locking = new System.Threading.Semaphore(1, 1);
            public static void WriteLog(string message,string file)
            {
                try
                {
                    locking.WaitOne();
                    StreamWriter log;
                    string _path = System.AppDomain.CurrentDomain.BaseDirectory;

                    string folder = _path + "/LogFolder";

                    bool folderExists = Directory.Exists(folder);
                    if (!folderExists)
                        Directory.CreateDirectory(folder);

                    string fileName = folder + "/" + file + "_" + DateTime.Today.ToString("yyyyMMdd") + ".txt";
                    if (!File.Exists(fileName))
                        log = new StreamWriter(fileName);
                    else
                        log = File.AppendText(fileName);
                    log.WriteLine(message);
                    log.WriteLine("------------------------------------------------------------");
                    log.Flush();
                    log.Close();

                }

                catch (Exception)
                {

                }

                finally
                {

                    locking.Release();

                }

            }

        }
    }
}